package cn.tedu.toa.management.sys.service.impl;

import cn.tedu.toa.management.ex.ServiceException;
import cn.tedu.toa.management.sys.entity.FKeyservices;
import cn.tedu.toa.management.sys.entity.SysUsers;
import cn.tedu.toa.management.sys.mapper.FKeyservicesMapper;
import cn.tedu.toa.management.sys.mapper.SysUsersMapper;
import cn.tedu.toa.management.sys.service.FKeyservicesService;
import cn.tedu.toa.management.sys.service.ex.DeleteException;
import cn.tedu.toa.management.sys.service.ex.ParamIllegelException;
import cn.tedu.toa.management.sys.service.ex.UpdateException;
import cn.tedu.toa.management.sys.vo.KeyServiceVo;
import cn.tedu.toa.management.util.UploadUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 * 特色服务表 服务实现类
 * </p>
 *
 * @author huaqian
 * @since 2021-10-02
 */
@Service
public class FKeyservicesServiceImpl extends ServiceImpl<FKeyservicesMapper, FKeyservices> implements FKeyservicesService {

    @Autowired
    private FKeyservicesMapper keyservicesMapper;

    @Autowired
    private SysUsersMapper usersMapper;

    private List<FKeyservices> keyservicesList = new CopyOnWriteArrayList<>();
    private Map<String,FKeyservices> keyservicesMap = new ConcurrentHashMap<>();
    @Override
    public List<FKeyservices> getMenus() {
        if(keyservicesList.isEmpty()){
            synchronized (keyservicesList){
                if(keyservicesList.isEmpty()){
                    List<FKeyservices> keyservices = keyservicesMapper.selectList(null);
                    for (FKeyservices k:keyservices
                    ) {
                        String username = usersMapper.selectById(k.getUserId()).getUsername();
                        k.setUsername(username);
                        keyservicesMap.put(k.getName(),k);
                    }
                    keyservicesList.addAll(keyservices);
                }
            }
        }
        return keyservicesList;
    }

    @Override
    public KeyServiceVo getServiceVo(String name) {
        if(keyservicesMap.isEmpty()){
            getMenus();
        }
        FKeyservices service = keyservicesMap.get(name);
        KeyServiceVo keyServiceVo = new KeyServiceVo()
                .setName(service.getName())
                .setContent(service.getContent())
                .setCreatedtime(service.getCreatedtime());
        int index = keyservicesList.indexOf(service);
        if(index==0){
            String prevName = keyservicesList.get(keyservicesList.size()-1).getName();
            String nextName = keyservicesList.get(index+1).getName();
            keyServiceVo.setPrevName(prevName).setNextName(nextName);
        }else if(index==keyservicesList.size()-1){
            String prevName = keyservicesList.get(index-1).getName();
            String nextName = keyservicesList.get(0).getName();
            keyServiceVo.setPrevName(prevName).setNextName(nextName);
        }else {
            String prevName = keyservicesList.get(index-1).getName();
            String nextName = keyservicesList.get(index+1).getName();
            keyServiceVo.setPrevName(prevName).setNextName(nextName);
        }
        return keyServiceVo;
    }

    @Override
    public PageInfo<FKeyservices> findKeyServicesList(String name, Integer pageNum, Integer pageSize) {
        //1 参数验证
        if(pageNum==null || pageSize==null || pageSize<1 ){
            throw new ParamIllegelException("服务列表查询异常!查询的分页参数不合法!");
        }
        //pageHelp分页
        PageHelper.startPage(pageNum,pageSize);
        //调用持久层
        List<FKeyservices> list=keyservicesMapper.selectKeyServicesList(name);
        //查询结果存入PageInfo
        return new PageInfo<>(list);
    }

    @Override
    public FKeyservices getService(String name) {
        if(keyservicesMap.isEmpty()){
            getMenus();
        }
        return keyservicesMap.get(name);
    }

    @Override
    public String uploadImg(MultipartFile imageFile) {
        try {
            UploadUtils uploadUtils = new UploadUtils();
            return uploadUtils.uploadImage(imageFile);
        } catch (IOException e) {
            throw new ServiceException("文件上传失败");
        }
    }

    @Override
    public void edit(FKeyservices keyService,String username) {
        keyService.setModifyusername(username);
        keyService.setModifytime(LocalDateTime.now());
        int num = keyservicesMapper.update(
                keyService.getType(), keyService.getName(), keyService.getImg(),
                keyService.getSummary(), keyService.getContent(),
                keyService.getModifyusername(),keyService.getModifytime(), keyService.getId()
        );
        if(num!=1){
            throw new UpdateException("修改失败");
        }
        keyservicesList = new CopyOnWriteArrayList<>();
        keyservicesMap = new ConcurrentHashMap<>();
        getMenus();

    }

    @Override
    public void insert(FKeyservices keyService, String userName) {
        SysUsers user = usersMapper.selectUserByName(userName);
        keyService.setUserId(user.getId())
                .setCreatedtime(LocalDateTime.now());
        keyservicesMapper.insert(keyService);
        keyservicesList = new CopyOnWriteArrayList<>();
        keyservicesMap = new ConcurrentHashMap<>();
        getMenus();
    }

    //删除
    @Override
    public void delete(Integer id) {
        int num = keyservicesMapper.deleteById(id);
        if(num!=1){
            throw new DeleteException("删除特色服务异常");
        }
    }
}
